'use strict';
var React = require('react');
var reactDom = require('react-dom/server');
var reactRedux = require('react-redux');
var configureStore = require('../public/javascripts/stores/index.js');
var HkfsFundDetail = require('../public/javascripts/containers/pc/HkfsFundDetail.jsx');
var Provider = reactRedux.Provider;
const store = configureStore();
var ModelRender = require('./modelRender.jsx');
var actions = require('../public/javascripts/actions/fundDetail');
var async = require('async');
var HttpOperate = require('../nodeLibrary/HttpOperate');
var backEndUrl=require('../nodeConfig/environmentConfig');
var httpOperate = new HttpOperate(backEndUrl.productBackEndUrl);
var fundNavHisList= require('../nodeLibrary/dataOperate/fundNavHisList');
var httpSelf=new HttpOperate();

function transactionExplain(orign, days) {
    var resultArray = [];
    var dic = {
        1: '一',
        2: '二',
        3: '三',
        4: '四',
        5: '五'
    };
    for (var i = 0; i < 5; i++) {
        var result = i + orign + days;
        if (result <= 5) {
            resultArray.push('周' + dic[result]);
        }
        else {
            resultArray.push('下周' + dic[result - 5])
        }
    }
    return resultArray;
}
//基金类型字典
var fundTypeDic={
    0:'其他类型',
    1:'股票型',
    2:'债券型',
    3:'混合型',
    4:'货币型',
    5:'保本型',
    6:'指数型',
    7:'QDII',
    8:'	商品型'
}

exports.RenderFunc = (fundCode, renderCallBack)=> {
    async.parallel({
        //基金基本信息
            fundNormalInfo: (callBack)=> {
                httpOperate.httpGet('/tlsGet?path=product/getFundInfo&params=fundCode=' + fundCode, (result)=> {
                    var resultContent = {
                        assetAmount: result.assetAmount,
                        setupDate: result.setupDate,
                        fundType: fundTypeDic[result.fundType],
                        fundFullName: result.fundFullName,
                        fundCode: result.fundCode,
                        fundCorp: result.fundCorp.name,
                        trustee: result.trustee,
                        fundName: result.fundName,
                        //前端认购费率
                        subscribeRate: result.subscribeRate,
                        //前端申购费率
                        allotRate: result.allotRate,
                        //赎回费率
                        redeemRate: result.redeemRate,
                        //管理费率
                        managerRate: result.managerRate,
                        //托管费率
                        trusteeRate: result.trusteeRate,
                        //申购确认（T+1）
                        confirmPaceArray: transactionExplain(result.confirmPace, 1),
                        //赎回到账（T+1）
                        refundPaceArray: transactionExplain(result.refundPace, 3),
                        //交易申请时间
                        transactionTime: ['上周五15点 周一15点', '周一15点～周二15点', '周二15点～周三15点', '周三15点～周四15点', '周四15点～周五15点']

                    };
                    callBack(null, resultContent);
                })
            },

        //净值数据
            fundNavHisList: (callBack)=> {
                httpOperate.httpPost('/fundnavHisList', {
                    'curPage': 1,
                    'pageSize': 10,
                    'fundCode': fundCode
                }, (result)=> {
                    //构造前端数据结构
                    var  fundNavHis={
                        page:1,
                        totalPage:Math.ceil(result.size / 10),
                        list:fundNavHisList.fundNavHisListDataOperate(result)
                    };
                    store.dispatch(actions.receivFundNavHislist(fundNavHis));
                    callBack();
                });
            },

            //基金基本信息
            fundValueInfo: (callBack)=> {
                httpSelf.httpPost('/pc/fundCtrl/fundInfoList', {
                    curPage: 1,
                    pageSize: 1,
                    fundCode: fundCode
                }, (result)=> {
                    var resultContent = {
                        nav: result.result[0].nav,
                        dayReturn: result.result[0].dayReturn

                    };
                    callBack(null, resultContent);
                });
            },
            //基金业绩排名
            fundPerformance: (callBack)=> {
                httpOperate.httpGet('/tlsGet?path=product/getFundPerformance&params=fundCode=' + fundCode, (result)=> {
                    store.dispatch(actions.receivFundPerformance(result));
                    callBack();
                })
            }
        },
        (err, results)=> {
            //合并基金基础信息
            var fundNormal=results.fundNormalInfo;
            fundNormal.nav=results.fundValueInfo.nav;
            fundNormal.dayReturn=results.fundValueInfo.dayReturn;
            store.dispatch(actions.receiveNormalInfo(fundNormal));


            var modelRender = new ModelRender();
            var main = reactDom.renderToString(
                <Provider store={store}>
                    <HkfsFundDetail/>
                </Provider>
            );
            var initializeStore = JSON.stringify(store.getState());
            renderCallBack(modelRender.headerRender(), modelRender.barRender(), modelRender.footerRender(), main, initializeStore);
        });
};